<template>
  <basic-container>
    <div class="basic-info-container">
      <el-page-header @back="toBack" :content="'基本信息'" class="return-page"></el-page-header>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="组织类型">
          {{ ruleForm.organize_type === 'company' ? '公司' : '个人' }}
        </el-form-item>
        <!-- <el-form-item label="所在国家">
          {{ userInfo.country }}
        </el-form-item> -->
        <el-form-item label="联系人" prop="contact_person">
          <el-input v-model="ruleForm.contact_person" :disabled="ruleForm.AccountType === 'platform'"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="ruleForm.mobile"></el-input>
        </el-form-item>
        <el-form-item label="证件号码" prop="tin">
          <el-input v-model="ruleForm.tin" :disabled="ruleForm.AccountType === 'platform'"></el-input>
        </el-form-item>
        <el-form-item style="color: #f56c6c;">
          <i class="el-icon-warning"></i>
          基本信息有更新，会重新发起基本信息审核，不影响已开通的功能实用
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>

    </div>
  </basic-container>
</template>

<script>
import { getAccountBaseInfo, updateAccountBaseInfo } from "@/api/user";
export default {
  
  data() {
    // 手机号
    const validateMobile = (rule, value, callback) => {
      const phoneReg = 11 && /^((13|14|15|16|17|18|19)[0-9]{1}\d{8})$/;
      if (!value) {
        callback(new Error("联系电话不可为空"));
      } else if (value.length <= 10 || !phoneReg.test(value)) {
        callback(new Error("联系电话格式不正确"));
      } else {
        callback();
      }
    };
    return {
      userInfo: {
        organize_type: '组织类型',
        country: '中国'
      },
      ruleForm: {
        name: '',
        organize_type: '',
        contact_person: '',
        mobile: '',
        tin: '',
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
        ],
        contact_person: [
          { required: true, message: '请输入联系人', trigger: 'blur' },
        ],
        mobile: [
          { required: true, validator: validateMobile, trigger: 'blur' },
        ],
        tin: [
          { required: true, message: '请输入证件号码', trigger: 'blur' },
        ],
      }
    }
  },
  computed: {},

  created() {
    this.getAccountBaseInfoFu();
  },

  methods: {
    // 获取账号信息
    getAccountBaseInfoFu() {
      getAccountBaseInfo()
        .then((res) => {
          const resData = res.data.data;
          // console.log('获取账号信息', resData);
          
          this.ruleForm = {...resData};
        })
    },
    // 保存账号信息
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            name: this.ruleForm.name,
            contact_person: this.ruleForm.contact_person,
            // mobile: this.ruleForm.mobile,
            tin: this.ruleForm.tin,
          }
          updateAccountBaseInfo(params)
            .then((res) => {
              const resData = res.data;
              console.log(120, '更新账号信息 ',resData);
              this.$message({
                type: "success",
                message: "更新成功!"
              });
            })

        } else {
          this.$message.warning('请完善基本信息!');
          return false;
        }
      });
    },
    // 取消保存
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 返回
    toBack() {
      this.$router.go(-1)
    }
  },
};
</script>

<style lang="scss" scoped>
.basic-info-container {
  color: #fff;

  .return-page {
    margin-bottom: 12px;
  }

  ::v-deep .el-icon-back {
    color: #fff !important;
  }

  ::v-deep .el-page-header__title {
    color: #fff !important;
  }

  ::v-deep .el-page-header__content {
    color: #fff !important;
  }

}
</style>
